package com.yhp.web;

import com.yhp.bean.Student;
import com.yhp.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = {"/Educational/student/addstudent"})
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //1.接收参数(剩下的可以自己去加)
        String sno = req.getParameter("sno");
        String sname = req.getParameter("sname");
        String sgradeid = req.getParameter("sgradeid");
        String ssex = req.getParameter("ssex");
        //2.调取方法
        StudentServiceImpl studentService = new StudentServiceImpl();
        Student student=new Student();
        student.setStuno(sno);
        student.setStuname(sname);
        student.setGid(Integer.parseInt(sgradeid));
        student.setSex(Integer.parseInt(ssex));
        int i = studentService.insertStu(student);
        //3.跳转页面
        if(i>0){
            resp.sendRedirect("/Educational/student/getstudents");
        }else {
            resp.sendRedirect("/Educational/student/getgrades");
        }
    }
}
